package view 
{
	import flash.display.MovieClip;
	import flash.geom.Point;
	import mx.core.UIComponent;
	
	/**
	 * Animation MXML compatible control
	 */
	public class AnimationAsset extends UIComponent 
	{
		////////////////////////////////////////////////////
		// 
		////////////////////////////////////////////////////
		
		private var _clip:MovieClip;
		private var _backColor:uint = 0xFFFFFF;
		
		////////////////////////////////////////////////////
		// 
		////////////////////////////////////////////////////
		
		public static const DEFAULT_WIDTH:Number = 300.0;
		public static const DEFAULT_HEIGHT:Number = 300.0;
		
		public static const SCALE_MIN_LIMIT:Number = 0.01;
		public static const SCALE_MAX_LIMIT:Number = 5.0;
		
		////////////////////////////////////////////////////
		// 
		////////////////////////////////////////////////////
		
		/**
		 * Constructor
		 */
		public function AnimationAsset() 
		{
			super();
			
			width = DEFAULT_WIDTH;
			height = DEFAULT_HEIGHT;
		}
		
		/**
		 * Animation
		 */
		public function set data(value:MovieClip):void
		{
			if (value === _clip)
				return;
			
			_clip = value;
			
			for (var i:int = numChildren - 1; i >= 0; i--)
				removeChildAt(i);
			
			if (_clip)
			{
				addChild(_clip);
				
				buttonMode = true;
				useHandCursor = true;
			}
			else
			{
				buttonMode = false;
				useHandCursor = false;
			}
		}
		
		/**
		 * Background color
		 */
		public function set backColor(value:uint):void
		{
			if (value == _backColor)
				return;
			
			_backColor = value;
			invalidateDisplayList();
		}
		
		/**
		 * Current animation position
		 */
		public function get animationPosition():Point
		{
			return _clip ? new Point(_clip.x, _clip.y) : null;
		}
		
		public function set animationPosition(value:Point):void
		{
			if (_clip && value)
			{
				_clip.x = value.x;
				_clip.y = value.y;
			}
		}
		
		/**
		 * Current animation scale value (from SCALE_MAX_LIMIT to SCALE_MIN_LIMIT)
		 */
		public function get animationScale():Number
		{
			return _clip ? _clip.scaleX : 1.0;
		}
		
		public function set animationScale(value:Number):void
		{
			if (isNaN(value) || value < SCALE_MIN_LIMIT) value = SCALE_MIN_LIMIT;
			if (value > SCALE_MAX_LIMIT) value = SCALE_MAX_LIMIT;
			
			if (_clip && _clip.scaleX != value)
			{
				_clip.scaleX = value;
				_clip.scaleY = value;
			}
		}
		
		////////////////////////////////////////////////////
		// 
		////////////////////////////////////////////////////
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			graphics.clear();
			
			if (!unscaledWidth || !unscaledHeight)
				return;
			
			graphics.beginFill(_backColor);
			graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
			graphics.endFill();
		}
	}

}